package ca.etsmtl.log660.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import ca.etsmtl.log660.configuration.SessionFactoryHelper;
import ca.etsmtl.log660.entity.Artiste;

class ArtisteDAO {
	
	private static final String FIND_ALL_ARTISTS = "Artiste.findAll";

	@SuppressWarnings("unchecked")
	public List<Artiste> getAll() {
		List<Artiste> artistes = null;
		
		Session session = SessionFactoryHelper.getSessionFactory().openSession();
		session.beginTransaction();
		artistes = session.getNamedQuery(FIND_ALL_ARTISTS).list();
		session.getTransaction().commit();
		session.close();
		
		if (artistes == null) {
			artistes = new ArrayList<Artiste>();
		}
		
		return artistes;
	}

	public Artiste getById(Long id) {
		Artiste artiste = null;
		
		Session session = SessionFactoryHelper.getSessionFactory().openSession();
		session.beginTransaction();
		artiste = (Artiste) session.get(Artiste.class, id);
		session.getTransaction().commit();
		session.close();

		return artiste;
	}
}
